package org.example.ai_chat.config;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;

import org.example.ai_chat.repository.CustomTableMysqlChatMemoryRepository;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


@Configuration
public class ChatConfig {

    @Value("${spring.chat.memory.repository.jdbc.mysql.jdbc-url}")
    private String mysqlJdbcUrl;

    @Value("${spring.chat.memory.repository.jdbc.mysql.driver-class-name}")
    private String mysqlDriverName;

    @Value("${spring.chat.memory.repository.jdbc.mysql.username}")
    private String mysqlUserName;

    @Value("${spring.chat.memory.repository.jdbc.mysql.password}")
    private String mysqlPassword;

    @Value("${spring.chat.memory.repository.jdbc.mysql.table-name}")
    private String tableName;

    /**
     * 设置数据源和JdbcTemplate
     */
    @Bean
    public JdbcTemplate jdbcTemplate() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(mysqlJdbcUrl);
        dataSource.setUsername(mysqlUserName);
        dataSource.setPassword(mysqlPassword);
        dataSource.setDriverClassName(mysqlDriverName);
        return new JdbcTemplate(dataSource);
    }

    /**
     * 默认存储
     */
    @Bean
    public MysqlChatMemoryRepository mysqlChatMemoryRepository(JdbcTemplate jdbcTemplate) {
        return MysqlChatMemoryRepository.mysqlBuilder().jdbcTemplate(jdbcTemplate).build();
    }

    /**
     * 自定义存储
     */
    @Bean
    public CustomTableMysqlChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate) {
        return CustomTableMysqlChatMemoryRepository.builder().jdbcTemplate(jdbcTemplate).tableName(tableName).build();
    }

    /**
     * 默认存储表：chat_ai_memory
     */
    @Bean
    public ChatClient defaultChatClient(ChatClient.Builder chatClientBuilder, MysqlChatMemoryRepository mysqlChatMemoryRepository) {
        return chatClientBuilder
                .defaultSystem("你现在是一个智能聊天机器人")
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(
                                MessageWindowChatMemory.builder()
                                        .chatMemoryRepository(mysqlChatMemoryRepository)
                                        .maxMessages(100)
                                        .build()
                        ).build()
                )
                .build();
    }

    /**
     * 自定义存储表：重写ChatMemoryRepository方法
     */
    @Bean
    public ChatClient selfChatClient(ChatClient.Builder chatClientBuilder, CustomTableMysqlChatMemoryRepository chatMemoryRepository) {
        return chatClientBuilder
                .defaultSystem("你现在是一个智能聊天机器人")
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .build()
                )
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(
                                MessageWindowChatMemory.builder()
                                        .chatMemoryRepository(chatMemoryRepository)
                                        .maxMessages(100)
                                        .build()
                        ).build()
                )
                .build();
    }


}
